window.addEventListener("load",function(){
    //获取元素
    var swiper = document.querySelector(".swiper");
    var hali = document.querySelectorAll(".hali li");
    var btns = document.querySelector(".btns");
    var leftbtn = document.querySelector(".leftbtn");
    var rightbtn = document.querySelector(".rightbtn");
    var leftBox = document.querySelector(".leftBox");
    var rightBox = document.querySelector(".rightBox");
    console.log(leftBox,rightBox);

    var index = 0;
    var timer;
    //清除图片类名
    function clearName(){
        for(var i = 0;i < hali.length;i++){
            hali[i].className = "";
        }
    }
    function leave(){
        timer = setInterval(function(){
            rightbtn.click();
        },2000);
    }

    var timer = setInterval(function(){
        rightbtn.click();
    },2000)

    swiper.addEventListener("mouseenter",function(){
        clearInterval(timer);
        swiper.removeEventListener("mouseleave",leave);
        swiper.addEventListener("mouseleave",leave);
    });

    leftbtn.addEventListener("click",function(){
        index --;
        index = index < 0 ? hali.length - 1 : index;
        var count = index;
        clearName();
        //上一张
        index = index == 0 ? hali.length : index;
        hali[index - 1].className = "prev";
        // 当前
        hali[index].className = "now";
        //  下一张
        index = index == hali.length - 1 ? -1  : index;
        hali[index + 1].className = "next";
        index = count;
    });

    rightbtn.addEventListener("click",function(){
        index ++;
        index = index == hali.length ? 0 : index;
        var count = index;
        clearName();
        //上一张
        index = index == 0 ? hali.length : index;
        hali[index - 1].className = "prev";
        index = count;
        //当前
        hali[index].className = "now";
        //下一张
        index = index == hali.length - 1 ? -1 : index;
        hali[index + 1].className = "next";
    })

    leftBox.addEventListener("click",function(){
        leftbtn.click();
    });

   rightBox.addEventListener("click",function(){
       rightbtn.click();
   }) ;


})